package com.itcam.tomcat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : Cammy.Wu
 * Description : 
 */

@WebServlet("/servletLife")
public class ServletLife extends HttpServlet {

    /**
     * 完成servlet对象的初始化工作
     * 在servlet接收到第一次请求的时候创建对象
     * 生命周期：从第一次接收请求开始到服务器关闭之后销毁
     * 当在web.xml中配置了load-on-startup属性，那么在服务器启动的时候就会创建servlet对象，中间的数值越小，优先级越高
     * @throws ServletException
     */
    @Override
    public void init() throws ServletException {
        System.out.println("init");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("servlet life");
        System.out.println("servlet life");
    }

    @Override
    public void destroy() {
        System.out.println("servlet life销毁了");
    }
}
